package com.charley.icloud.admin.webapps.sysdict.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.charley.icloud.admin.webapps.sysdict.pojo.entity.SysDictDO;
import com.charley.icloud.admin.webapps.sysdict.pojo.request.SysDictQuery;
import com.charley.icloud.admin.webapps.sysdict.service.SysDictService;
import com.charley.icloud.common.constants.SystemNumberConstants;
import com.charley.icloud.common.response.result.HttpResponseResult;
import com.charley.icloud.common.response.result.HttpResponseResultFactory;
import com.charley.icloud.datasource.page.PageRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sys/dict")
@Tag(name = "字典控制器")
public class SysDictController {

    @Resource
    private SysDictService sysDictService;

    @PostMapping(value = "/save")
    @Operation(summary = "保存字典", description = "后台管理系统")
    public HttpResponseResult save(@RequestBody SysDictQuery query) {
        if (query != null) {
            SysDictDO record = new SysDictDO();
            BeanUtil.copyProperties(query, record, CopyOptions.create().ignoreCase().ignoreNullValue());
            return HttpResponseResultFactory.success(sysDictService.save(record));
        }
        return HttpResponseResultFactory.fail("字典保存失败");
    }

    @PostMapping(value = "/delete")
    @Operation(summary = "删除字典", description = "后台管理系统")
    public HttpResponseResult delete(@RequestBody List<SysDictQuery> queries) {
        List<SysDictDO> records = new ArrayList<>();
        if (queries != null && queries.size() > SystemNumberConstants.SYSTEM_NUMBER_INTEGER_ZERO) {
            for (SysDictQuery query : queries) {
                SysDictDO sysDictDO = new SysDictDO();
                BeanUtil.copyProperties(query, sysDictDO, CopyOptions.create().ignoreCase().ignoreNullValue());
                records.add(sysDictDO);
            }
            return HttpResponseResultFactory.success(sysDictService.delete(records));
        }
        return HttpResponseResultFactory.fail("字典删除失败...");
    }

    @PostMapping(value = "/findPage")
    @Operation(summary = "分页查询", description = "后台管理系统")
    public HttpResponseResult findPage(@RequestBody PageRequest pageRequest) {
        return HttpResponseResultFactory.success(sysDictService.findPage(pageRequest));
    }

    @GetMapping(value = "/findByLabel")
    @Operation(summary = "根据标签查询", description = "后台管理系统")
    @Parameter(name = "label", description = "标签名称", required = true, in = ParameterIn.QUERY)
    public HttpResponseResult findByLabel(@RequestParam String label) {
        return HttpResponseResultFactory.success(sysDictService.findByLabel(label));
    }
}
